%------------------------------------------------------------------
%------------------------------------------------------------------
%   Author: Md. Sazzad Hussain (sazzad.hussain@sydney.edu.au)
%   Learning and Affect Technologies Engineering
%   University of Sydney, 2011
%------------------------------------------------------------------
%------------------------------------------------------------------

function [arousal_self,cogLoad_self,cogLoad_score,cogLoad_resp,arousal_cat,cogLoad_cat,time_stamp,featExt]=siento_EmoCogSREx(subjectID,filesPath,featdir,opSys)
% %this function extracts arousal & CL self rating & performance from subjects

if opSys==1;
    sep='/';
elseif opSys==2
    sep='\';
end

filesDir=dir(filesPath);%dir info for files path
x=1;

for n=1:length(subjectID(:,1))
    for m=1:length(filesDir)
        [path,name,ext,ver] = fileparts(filesDir(m).name);
        if strcmp(subjectID(n,:),name)%choose only selected subjects
            loadAnn(x,:)=[filesPath sep name ext];%self report log
        elseif strcmp(strcat(subjectID(n,:),'_score'),name)%score log
            loadScore(x,:)=[filesPath sep name ext];
            x=x+1;
        end
    end
end

arSelfAll=[];%arousal(self) all subjects
arCatAll=[];%arousal(cat) all subjects
taskSelfAll=[];%task(self) all subjects
taskCatAll=[];%task(cat) all subjects
ansAll=[];%score all subjects 
respAll=[];%resp time all subjects
tStampAll=[];%time stamp
for j=1:(x-1)
    fid = fopen(loadAnn(j,:));%load annotation
    C = textscan(fid,'%s%s%s%s%s%s%s%s%s%s%s%s','delimiter', ',');
    fclose(fid);
    
    fid2 = fopen(loadScore(j,:));%load scores
    C_score = textscan(fid2,'%s%s%s%s','delimiter', ',');
    fclose(fid2);

    %annotation
    emotionCat=C{2};    %given emotion
    taskLevel=C{3};     %given task level
    startT=C{4};        %start of task
    endT_NT=C{6};       %end image(no tasks)
    selfTask=C{9};      %report task
    selfEmotion=C{10};  %report emotion
    
    %scores
    refTime_score=C_score{1}; %ref time in score
    score_acc=C_score{2};%correct ans
    score_sub=C_score{3};%reported ans
    responseT=C_score{4};%response time
    respT=str2num(char(responseT));%resp time in num
    mean_respT=median(respT);%mean resp time for subject
    
    lenFile=length(C{1}(:,1));
    emotionCat=emotionCat(3:(lenFile-1)); %given emotion
    taskLevel=taskLevel(3:(lenFile-1)); %given task level
    startT=vertcat(startT(3:12), endT_NT(13:24), startT(25:(lenFile-1))); %start of task
    selfTask=selfTask(3:(lenFile-1));       %-->report task
    selfEmotion=selfEmotion(3:(lenFile-1)); %-->report emotion
    
    for t=1:length(startT)
        startTime=char(startT{t});
        if t<=10 | t>=23
            start_times(t) = datenum(startTime,'HH:MM:SS')*24*60*60; % in seconds 
            sTime(t)={datestr((start_times(t)/(24*60*60)),'HH:MM:SS')};%back to actual time
        else
            start_times(t) = (datenum(startTime,'HH:MM:SS')*24*60*60)-14; % in seconds
            sTime(t)={datestr((start_times(t)/(24*60*60)),'HH:MM:SS')};%back to actual time
        end
    end
    
    k = 1;
    arSelfVec=[];
    arCatVec=[];
    taskSelfVec=[];
    taskCatVec=[];
    taskTimeVec=[];
    while k <= length (start_times)
        taskTimeVec=[taskTimeVec;sTime(k)];
        %extracting theoretical arousal
        if strcmp(emotionCat(k), '0-NoP_Task')
            arCat=0;
        elseif strcmp(emotionCat(k), '1-Pic_NoT')
            arCat=9;
        elseif strcmp(emotionCat(k), '2-HV_LA')
            arCat=1;
        elseif strcmp(emotionCat(k), '3-HV_MA')
            arCat=2;
        elseif strcmp(emotionCat(k), '4-HV_HA')
            arCat=3;
        elseif strcmp(emotionCat(k), '5-LV_LA')
            arCat=1;
        elseif strcmp(emotionCat(k), '6-LV_MA')
            arCat=2;
        elseif strcmp(emotionCat(k), '7-LV_HA')
            arCat=3;
        end        
        %extracting self reports arousal
        if strcmp(selfEmotion(k),'1')
            arSelf=1;
        elseif strcmp(selfEmotion(k),'2')
            arSelf=1;
        elseif strcmp(selfEmotion(k),'3')
            arSelf=2;
        elseif strcmp(selfEmotion(k),'4')
            arSelf=2;
        elseif strcmp(selfEmotion(k),'5')
            arSelf=3;
        elseif strcmp(selfEmotion(k),'6')
            arSelf=3;
        elseif strcmp(selfEmotion(k),'7')
            arSelf=3;
        elseif strcmp(selfEmotion(k),'8')
            arSelf=2;
        elseif strcmp(selfEmotion(k),'9')
            arSelf=1;
        else
            arSelf=0;
        end
        %extracting self reports (CL)
        if strcmp(selfTask(k),'1') |  strcmp(selfTask(k),'2') | strcmp(selfTask(k),'3')
            taskSelf=1;
        elseif strcmp(selfTask(k),'4') | strcmp(selfTask(k),'5') | strcmp(selfTask(k),'6')
            taskSelf=2;
        elseif strcmp(selfTask(k),'7') | strcmp(selfTask(k),'8') | strcmp(selfTask(k),'9')
            taskSelf=3;
        else
            taskSelf=0;
        end
        %extracting given (CL)
        if strcmp(selfTask(k),'NA') |  strcmp(selfTask(k),'1')
            taskcat=1;
        elseif strcmp(selfTask(k),'2') | strcmp(selfTask(k),'3')
            taskcat=2;
        elseif strcmp(selfTask(k),'4') | strcmp(selfTask(k),'5')
            taskcat=3;
        else
            taskcat=0;
        end
        
        arSelfVec = [arSelfVec;arSelf];%ar_self vec for 1 sub
        arCatVec=[arCatVec;arCat];%ar_cat vec for 1 sub
        taskSelfVec=[taskSelfVec,taskSelf];%task_self vec for 1 sub
        taskCatVec=[taskCatVec,taskcat];%task_cat vec for 1 sub
        
        k = k+ 1;
    end
    
    ansVec=[];
    respVec=[];
    ansTimeVec=[];
    %extracting task scores
    scoreIndex=0;
    for r=1:length(refTime_score)
        %         score_acc(r)
        if ~strcmp(score_acc(r),'11')
            scoreIndex=scoreIndex+1;
            if scoreIndex>10
                if strcmp(score_acc(r),score_sub(r))
                    ans=1;%correct
                elseif ~strcmp(score_acc(r),score_sub(r))
                    ans=0;%wrong
                end
                
                if respT(r)<= mean_respT
                    resp=1;%low resp time
                elseif respT(r)> mean_respT
                    resp=2;%high resp time
                end
                
                ansVec=[ansVec;ans];
                respVec=[respVec;resp];
                ansTimeVec=[ansTimeVec;refTime_score(r)];
            end
        end
    end

    % %filter data
    n=1;
    for m=1:length(arSelfVec)
        if arSelfVec(m)>0 & taskSelfVec(m)>0%rule for filter
%         if arCatVec(m)>0 & taskCatVec(m)>0%rule for filter
            ar_self1(n)=arSelfVec(m);
            cl_self1(n)=taskSelfVec(m);
            ar_cat1(n)=arCatVec(m);
            cl_cat1(n)=taskCatVec(m);
            task_time1(n)=taskTimeVec(m);
            n=n+1;
        end
    end
    
    %convert to number format
    ansTime1=datenum(ansTimeVec,'HH:MM:SS');
    taskTime1=datenum(task_time1,'HH:MM:SS');
    %convert back to date format
    ansTime2=datestr(ansTime1,'HH:MM:SS');
    taskTime2=datestr(taskTime1,'HH:MM:SS');  
    
    s=0;
    for p=1:length(ansTime2)
        for q=1:length(taskTime2)
            if strcmp(ansTime2(p,:),taskTime2(q,:))
%                 if ~strcmp(ansTime2(p,:),ansTime2((p+1),:)) 
                    s=s+1;
                    ar_self2(s)=ar_self1(q);
                    cl_self2(s)=cl_self1(q);
                    ar_cat2(s)=ar_cat1(q);
                    cl_cat2(s)=cl_cat1(q);            
                    cogLoad_score2(s)=ansVec(p);
                    resp_time1(s)=respVec(p);
                    ansTime3{s}=ansTime2(p,:);
%                 end
            end
        end
    end     
    arSelfAll = [arSelfAll,ar_self2];
    arCatAll=[arCatAll,ar_cat2];
    taskSelfAll=[taskSelfAll,cl_self2];%task report vector
    taskCatAll=[taskCatAll,cl_cat2];%task level vector
    ansAll=[ansAll,cogLoad_score2];
    respAll=[respAll;resp_time1];
    tStampAll=[tStampAll,ansTime3];
end

arousal_self=arSelfAll';
cogLoad_self=taskSelfAll';
cogLoad_score=ansAll';
cogLoad_resp=respAll';
arousal_cat=arCatAll';
cogLoad_cat=taskCatAll';
time_stamp=tStampAll';

featExt=1;%end